package bfa.playlist_importer_exporter;

import java.io.IOException;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.widget.Toast;

public class ExportPlaylist {

	public static ArrayList<Playlist> getPlayLists(Context context) {
		ArrayList<Playlist> res=new ArrayList<Playlist>();
		
		String[] proj = {"*"};
        Uri tempPlaylistURI = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
        Cursor playListCursor= ((Activity) context).managedQuery(tempPlaylistURI, proj, null,null,null);

        if(playListCursor == null){
            //System.out.println("Not having any Playlist on phone --------------");
            return res;//don't have list on phone
        }
        
        
        for(int i = 0; i <playListCursor.getCount() ; i++) {
            playListCursor.moveToPosition(i);

            Playlist p=new Playlist();
            p.id=playListCursor.getInt(playListCursor.getColumnIndex("_id"));
            p.name=playListCursor.getString(playListCursor.getColumnIndex("name"));
        	res.add(p);            
        }
        
        return res;
	}
	
	public static void exportPlaylists(Context context,ArrayList<Playlist> playlists) {
			String[] proj = {"*"};
			//ArrayList<Playlist> playlists=getPlayLists(context);
			
	        JSONObject obj=new JSONObject();
	        JSONArray playlistsJson = new JSONArray();
	        
	        try {
				obj.put("playlists", playlistsJson);
		        
		        for(Playlist playlist:playlists) {
		        	if (playlist.selected) {
			            JSONObject playlistJson=new JSONObject();
		                playlistsJson.put(playlistJson);
		                
		                playlistJson.put("name",playlist.name);
		                playlistJson.put("_id",playlist.id);
			        	
			        
			
	
			            JSONArray songsJson = new JSONArray();
			            playlistJson.put("songs",songsJson);
			            
			            
			            Uri membersUri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlist.id);
			            Cursor membersCursor = ((Activity) context).managedQuery(membersUri, proj, null, null, null);
			            if(membersCursor  == null){
			            	//System.out.println("Not having any song in the playlist --------------");
			            } else {
			            	 for(int s = 0; s <membersCursor.getCount() ; s++) {
			            		 JSONObject songJ=new JSONObject();
			            		 songsJson.put(songJ);
			            		 
			            		 membersCursor.moveToPosition(s);
			                     for (int d=0;d<membersCursor.getColumnCount();d++) {
			                     	String cname2=membersCursor.getColumnName(d);
			                     	//System.out.println("    > " + cname2 + "  : " + membersCursor.getString(d));
			                     	if (! cname2.equals("title_key")) {
			                     		songJ.put(cname2, membersCursor.getString(d));	
			                     	}
			                     	
			                     }
			                     
			            	 }
			            }
			
		        	}        
		            //playListName = playListCursor.getString(playListCursor.getColumnIndex("name"));
		            
		        }
		        
		        //System.out.println(obj.toString(4));
		      
		        try {
		            Helping.createCachedFile(context,
		                            "export_playlists.json.txt", obj.toString(4));
		 
		            context.startActivity(Helping.getSendEmailIntent(
		            		context,
		                            "fbrisa@gmail.com", "Playlist export",
		                            "See attached", "export_playlists.json.txt"));
		        } catch (IOException e) {
		            e.printStackTrace();
		        } catch (ActivityNotFoundException e) {
		            Toast.makeText(context,
		                "Gmail is not available on this device.",
		                Toast.LENGTH_SHORT).show();
		        }
		        
			} catch (JSONException e) {
				e.printStackTrace();
			
			}

//	        if(playListCursor != null)
//	            if (! playListCursor.isClosed())
//	            	playListCursor.close();

	    	
	    }
	
}
